<?php
/**
 * 团购抢购
 *
 * by abc.com 多用户商城
 *
 *
 */
//by abc.com
//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');
class groupbuyControl extends mobileHomeControl{
	public function __construct() {
        parent::__construct();
    }

    //即将开团的团购列表
    public function groupbuy_soon_listOp(){
    	$data=$this->getlist('团购','soon');
    	output_data(array('groupbuy_soon_list'=>$data));
    }

    //正在团购的团购列表
    public function groupbuy_listOp(){
    	$data=$this->getlist('团购','now');
    	output_data(array('groupbuy_list'=>$data));
    }
    
    /*
     * 20170105周家财新增
     * 显示团购活动列表
     */
    public function show_groupbuy_listOp(){
        $groupbuy=$this->getlist('团购','now');
        
        //把团长价和团员价加到数据中
        $groupbuy_team_template_model=Model('groupbuy_team_template');
        
        
        $temp_groupbuy=array();
        //把商品小图传上来，同时减少字段
        $goods_model=Model('goods');//getGoodsInfo
        foreach ($groupbuy as $key => $val){
            $temp_groupbuy[$key]['groupbuy_id']=$groupbuy[$key]['groupbuy_id'];
            $team_template=$groupbuy_team_template_model->get_template($temp_groupbuy[$key]);
            $temp_groupbuy[$key]['groupbuy_lead_price']=$team_template['team_template_lead_price'];
            $temp_groupbuy[$key]['groupbuy_member_price']=$team_template['team_template_member_price'];
            $temp_groupbuy[$key]['groupbuy_team_num']=$team_template['team_template_num'];
            $temp_groupbuy[$key]['goods_id']=$groupbuy[$key]['goods_id'];
            $temp_groupbuy[$key]['goods_name']=$groupbuy[$key]['goods_name'];
            $temp_groupbuy[$key]['goods_price']=$groupbuy[$key]['goods_price'];
            $temp_groupbuy[$key]['count_down']=$groupbuy[$key]['count_down'];
            $temp_groupbuy[$key]['groupbuy_id']=$groupbuy[$key]['groupbuy_id'];
            
            $condition['goods_id']=$groupbuy[$key]['goods_id'];
            $goods_info=$goods_model->getGoodsDetail($condition['goods_id']);
            $temp_groupbuy[$key]['goods_img']=$goods_info['goods_image_mobile'];
            
        }

        output_data(array('groupbuy_list'=>$temp_groupbuy));
        
    }
    
    //20170110周家财新增 S
      /*
     * 该团购商品下的所有拼团
     */
    
    public function team_listOp(){
        $condition['groupbuy_id']=$_REQUEST['groupbuy_id'];
        $condition['pay_status']=array('gt',0);
        $limit=0;
        $groupbuy_team_model=Model('groupbuy_team');
        $team_list=$groupbuy_team_model->get_team_list($limit,$condition);
        $member_model=Model('member');
        $condition=array();
        foreach ($team_list as $key =>$val){
            $condition['member_id']=$val['lead_id'];
            $member_info=$member_model->getMemberInfo($condition);
            if (!empty($member_info['member_avatar'])){
                    $team_list[$key]['lead_avatar']=getMemberAvatarForFilePath($member_info['member_avatar']);
                }else{
                    $team_list[$key]['lead_avatar']=UPLOAD_SITE_URL.'/'.ATTACH_AVATAR.'/default_avatar.png';
                }
            }
        
        
        output_data($team_list);
    }
    
    
    //20170110周家财新增 E
    
    
    
    
    
    
    
    //20170110周家财新增S
   
    
    /*
     * 该拼团下的所有团员列表
     */
    public function team_memberOp(){
        /* if (!empty($_POST['member_id']) || $_POST['member_id']!=null || $_POST['member_id']!=''){
            $condition['member_id']=$_POST['member_id'];
        } */
        $condition['team_id']=$_POST['team_id'];
        $condition['pay_status']=1;
        $groupbuy_mem_model=Model('groupbuy_mem');
        $result=$groupbuy_mem_model->member_list($condition);
        output_data(array('member_list'=>$result));
    }
    
    //20170110周家财新增E
    
    
    /**
     * 判断拼团商品库存
     */
    public function check_group_numOp(){
        //商品详情
        if (empty($_REQUEST['goods_id'])  || empty($_REQUEST['groupbuy_id'])){
            output_data(array('status'=>'error','msg'=>'参数不足'));
        }
         
        $goods_id=intval($_REQUEST['goods_id']);
        $goods_model=Model('goods');
        $goods_info=$goods_model->getGoodsDetail($goods_id);
        
        //echo json_encode($goods_info['goods_info']['goods_storage']);die;
        if ($goods_info['goods_info']['goods_storage'] <=3){
            output_data(array('status'=>'error','msg'=>'商品库存不足'));
        }else{
            output_data(array('status'=>'success','msg'=>'OK'));
        }
    }
    
    //20170105周家财修改S
    /*
     * 团购商品详情
     */
    public function groupbuy_goods_detailOp(){
       
        //商品详情
        if (empty($_REQUEST['goods_id'])  || empty($_REQUEST['groupbuy_id'])){
            output_data(array('status'=>'error','msg'=>'参数不足'));
        }
       
        $goods_id=intval($_REQUEST['goods_id']);
        $goods_model=Model('goods');
        $goods_info=$goods_model->getGoodsDetail($goods_id);

//echo json_encode($goods_info['goods_info']['goods_storage']);die; 
        if ($goods_info['goods_info']['goods_storage'] <=3){
            output_data(array('status'=>'error','msg'=>'商品库存不足'));
        }
        
    	//添加拼团商品分享URL
    	$goods_info['groupbuy_url']=urlShop('groupbuy','groupbuy_goods_detail',array('groupbuy_id' => $_REQUEST['groupbuy_id'],'goods_id'=>$_REQUEST['goods_id']));
    	$goods_info['groupbuy_url']=str_replace('/shop/shop/', '/shop/mobile/', $goods_info['groupbuy_url']);

        
        //店铺信息2017-01-23 S
        $model_store = Model('store');
        $store_info = $model_store->getStoreInfoByID($goods_info['goods_info']['store_id']);
        
        $goods_info['store_info']['store_id'] = $store_info['store_id'];
        //20161223 周家财修改  ----店铺名称先修改为品牌名,后面还要改回来
        $goods_info['store_info']['store_name'] = $goods_info['goods_info']['store_name'];
        //$goods_detail['store_info']['store_name'] = $store_info['store_name'];
        $goods_info['store_info']['member_id'] = $store_info['member_id'];
        //显示QQ及旺旺 多用户商城
        $goods_info['store_info']['store_qq'] = $store_info['store_qq'];
        $goods_info['store_info']['store_ww'] = $store_info['store_ww'];
        $goods_info['store_info']['store_phone'] = $store_info['store_phone'];
        $goods_info['store_info']['member_name'] = $store_info['member_name'];
        
        $goods_info['store_info']['avatar'] = getMemberAvatarForID($store_info['member_id']);
        //var_export($goods_info);die;
        if(intval($goods_info['goods_info']['is_pick']) > 0) {
            $setting = Model("setting")->getListSetting();
            $goods_info['goods_info']['pick_address'] = $setting['delivery_area_info'].$setting['delivery_address'];
        }
        
        //商品详细信息处理
        //$goods_detail = $this->_goods_detail_extend($goods_detail);
        //店铺信息2017-01-23 E
        
        //查找所有拼图
        //$condition['groupbuy_id']=$this->group_id;
        $groupbuy_team_model=Model('groupbuy_team');
        $condition_template['groupbuy_id']=$condition['groupbuy_id']=$_REQUEST['groupbuy_id'];
        $condition['end_time']=array('gt',TIMESTAMP);
        $condition['pay_status']=array('gt',0);
        $limit=2;
        $team_list=$groupbuy_team_model->get_team_list($limit,$condition);
        //查找用户头像

        //$member_info['avatar'] = getMemberAvatarForFilePath($this->member_info['member_avatar']);
        $member_model=Model('member');

        foreach ($team_list as $key_team =>$val_team){
            $member_id=$team_list[$key_team]['lead_id'];
            $member_info=$member_model->getMemberInfoByID($member_id);
            if (!empty($member_info['member_avatar'])){
                $team_list[$key_team]['lead_avatar']=getMemberAvatarForFilePath($member_info['member_avatar']);
            }else{                  //http://localhost/lxs/data/upload   //shop/avatar/
                $team_list[$key_team]['lead_avatar']=UPLOAD_SITE_URL.'/'.ATTACH_AVATAR.'/default_avatar.png';
            }
            
        }
        
        //查找到团长价和团员价
        $groupbuy_team_template_model=Model('groupbuy_team_template');
        $team_template=$groupbuy_team_template_model->get_template($condition_template);
        $goods_info['goods_info']['groupbuy_lead_price']=$team_template['team_template_lead_price'];
        $goods_info['goods_info']['groupbuy_member_price']=$team_template['team_template_member_price'];
        $goods_info['goods_info']['team_num']=$team_template['team_template_num'];
        
        $goods_info['team_list']=$team_list;

        //查找地区，只需要两级，第一级省，第二级市
        $model_area = Model('area');
        $condition_pri['area_deep']=1;
        $condition_pri['area_id']=array('lt', 32);
        $area_province = $model_area->getAreaList($condition_pri, 'area_id,area_name,area_parent_id');
        
        $condition_city['area_deep']=2;
        $condition_city['area_parent_id']=array('lt', 32);
        $area_city = $model_area->getAreaList($condition_city, 'area_id,area_name,area_parent_id');
        
        output_data($goods_info,array('area_province'=>$area_province,'area_city'=>$area_city));
        //output_data($goods_info);
    
    }
    //20170105周家财修改E

  
    //即将开团的抢购列表
    public function panicbuy_soon_listOp(){
    	$data=$this->getlist('抢购','soon');
    	output_data(array('panicbuy_soon_list'=>$data));
    }

    //正在团购的抢购列表
    public function panicbuy_listOp(){
    	$data=$this->getlist('抢购','now');
    	output_data(array('panicbuy_list'=>$data));
    }
    /*
     * 团购活动详情
     */
    public function groupbuy_detailOp(){
    	$groupbuy_id=intval($_REQUEST['groupbuy_id']);
    	$groupbuy=Model('groupbuy');
    	//根据抢购编号读取抢购信息
    	$data=$groupbuy->getGroupbuyInfoByID($groupbuy_id);
    	output_data($data);
    }
   
    
    /**
     *
     * @param unknown $gc_name
     * @param unknown $type
     */
    public function getlist($gc_name,$type){
    	$g_class=new Model();
    	
    	//$g_c_id=$g_class->table('groupbuy_class')->where(array('class_name'=>'团购'))->find();
    	$g_c_id=$g_class->table('groupbuy_class')->getfby_class_name($gc_name,'class_id');
       
    	$groupbuy=Model('groupbuy');
    	$condition['class_id']=$g_c_id;
    	if($type=='soon'){
    	    //读取即将开始的抢购列表
    		$data=$groupbuy->getGroupbuySoonList($condition);

    	}
    	if($type=='now'){
    	    //读取当前可用的抢购列表
			$data=$groupbuy->getGroupbuyOnlineList($condition);
			foreach ($data as $key =>$value){
			     foreach ($value as $k=>$val){
			         if ($k == 'groupbuy_url'){
			             $data[$key]['groupbuy_url']=str_replace('/shop/', '/mobile/', $val);
			         }
			         if($k == 'goods_url'){
			             $data[$key]['goods_url']=str_replace('/shop/', '/mobile/', $val);
			         }
			     }
			}
    	}
      
        for($i=0;$i<count($data);$i++){
           
            $data[$i]['groupbuy_image1']=gthumb($data[$i]['groupbuy_image1'], 'max');
            $data[$i]['groupbuy_image']=gthumb($data[$i]['groupbuy_image'], 'max');
        }
       
    	return $data;
    }
}